//
//  CZMainTabBar.swift
//  01-sinaWeibo-05-09
//
//  Created by mac on 16/5/12.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit


class CZMainTabBar: UITabBar {
    
    //MARK: - 闭包属性
    //2.加号按钮调用闭包
    var compseCallback: (()->())?
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //先计算每一个按钮的frame的宽度
        let width = self.frame.width/5
        
        //记录每一个控件的下标
        var index  = 0
        //
        for view in self.subviews{

            if view.isKindOfClass(NSClassFromString("UITabBarButton")!)
            {
                //能到这里面来说明 VIew 是 UITabBarButton
                //重新设置frame
                view.frame = CGRect(x: CGFloat(index)*width, y: 0, width: width, height: self.frame.height)
                index++
                if index  == 2 {
                    index++
                }
            }
        }
        composeButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: self.frame.height)
    }
    //MARK: - 按钮点击事件
    func composeButtonClick(button: UIButton){
        
         // 最终需要去Modal出控制器,View不能Modal出控制器,只有控制器才能Modal出控制器,当按钮点击的时候需要让控制器去做事情
        //调用闭包
        compseCallback?()
        
    }

    //MARK: - 懒加载
    
    lazy var composeButton : UIButton = {
        //创建按钮
        let button = UIButton(type: UIButtonType.Custom)
        //设置按钮
        //背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        //按钮图片
        
        let image = UIImage(named:"tabbar_compose_icon_add")
        button.setImage(image, forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        self.addSubview(button)
        
        button.addTarget(self, action: "composeButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()

}
